package com.hnichr.ump.talent.library.provider.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ump.common.talent.library.entity.EducationInfo;
import com.hnichr.ump.common.talent.library.entity.Resume;
import com.hnichr.ump.common.talent.library.entity.SchoolList;
import com.hnichr.ump.common.talent.library.handle.SchoolTypeHandle;
import com.hnichr.ump.common.talent.library.service.EducationInfoService;
import com.hnichr.ump.common.talent.library.service.ResumeService;
import com.hnichr.ump.common.talent.library.service.SchoolListService;
import com.hnichr.ump.talent.library.provider.SyncPolicyProvider;
import com.mxpioframework.jpa.JpaUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class EducationInfoSyncPolicyProvider implements SyncPolicyProvider {

    private static final Logger logger = LoggerFactory.getLogger(EducationInfoSyncPolicyProvider.class);

    private static final String KEY = "eduInfoList";

    @Autowired
    private EducationInfoService educationInfoService;

    @Autowired
    private SchoolListService schoolListService;

    @Autowired
    private ResumeService resumeService;

    @Override
    public boolean support(JSONObject jsonObject) {
        return jsonObject.containsKey(KEY);
    }

    @Override
    @Transactional
    public void sync(String syncDate, JSONObject jsonObject) {
        JSONArray eduInfoList = jsonObject.getJSONArray(KEY);
        if(CollectionUtils.isEmpty(eduInfoList)){
            return;
        }
        for(Object eduObject:eduInfoList){
            if(eduObject instanceof JSONObject){
                JSONObject edu = (JSONObject) eduObject;
                EducationInfo educationInfo = new EducationInfo();
                educationInfo.setId(edu.getString("id"));
                educationInfo.setDelFlag(edu.getBoolean("delFlag"));
                educationInfo.setUserId(edu.getString("userId"));
                if(edu.containsKey("createTime")){
                    educationInfo.setOrginalCreateTime(new Date(edu.getLong("createTime")));
                }

                if(edu.containsKey("updateTime")){
                    educationInfo.setOrginalUpdateTime(new Date(edu.getLong("updateTime")));
                }

                educationInfo.setDegree(edu.getString("r1"));
                educationInfo.setFullTime(edu.getString("r2"));
                educationInfo.setSchoolName(edu.getString("schoolName"));
                educationInfo.setEduLevel(edu.getInteger("eduLevel"));
                educationInfo.setRecruitmentFlag(edu.getInteger("recruitmentFlag"));
                educationInfo.setSchoolLevel(edu.getInteger("schoolLevel"));
                educationInfo.setMajor(edu.getString("major"));
                if(edu.containsKey("startEduTime")){
                    educationInfo.setStartTime(new Date(edu.getLong("startEduTime")));
                }
                if(edu.containsKey("endEduTime")){
                    educationInfo.setEndTime(new Date(edu.getLong("endEduTime")));
                }
                educationInfo.setResumeId(edu.getString("resumeId"));
                educationInfo.setSyncDate(syncDate);
                educationInfoService.merge(educationInfo);
            }
        }
    }

    @Override
    @Transactional
    public void postSync(String syncDate){
        Set<String> resumeSet = new HashSet<>();
        //edu更新的要同步更新resume
        List<EducationInfo> eduList = educationInfoService.getListBySyncDate(syncDate);
        if(CollectionUtils.isEmpty(eduList)){
            return;
        }
        List<SchoolList> schoolLists = schoolListService.listAll();
        SchoolTypeHandle handle = new SchoolTypeHandle();
        handle.init(schoolLists);
        for(EducationInfo educationInfo:eduList){
            if(resumeSet.contains(educationInfo.getResumeId())){
                continue;
            }
            Resume resume = resumeService.getById(educationInfo.getResumeId());
            //同步日期一样，在简历的同步代码里已经处理，此处跳过不重复更新
            if(StringUtils.equals(resume.getSyncDate(),syncDate)){
                resumeSet.add(resume.getId());
                continue;
            }

            List<EducationInfo> resumeEduList =  educationInfoService.getListByResumeId(resume.getId());
            resume.setSchoolType(handle.classify(resumeEduList));
            JpaUtil.update(resume);
            resumeSet.add(resume.getId());
        }
    }
}
